//
//  VisitorView.swift
//  DSWeiBo
//
//  Created by iOS聚商 on 2017/6/28.
//  Copyright © 2017年 dreamSun. All rights reserved.
//

import UIKit

//Swift 中如何定义协议
protocol VisitorViewDelgate:NSObjectProtocol{
    
    //登录回调
    func loginBtnClick()
    
    //注册回调
    func registBtnClick()
}


class VisitorView: UIView {

   //定义一个属性保存代理,一定要加weak防止循环引用
   weak var delegate:VisitorViewDelgate?
    
    
    /// 设置未登录界面
    ///
    /// - Parameters:
    ///   - isHome: 是否是首页
    ///   - imageName: 需要展示的图标名称
    ///   - message: 需要展示的文本内容
    func setupVisitorInfo(isHome:Bool,imageName:String,message:String)
    {
        
        iconView.isHidden = !isHome
        homeIcon.image = UIImage(named: imageName)
        messageLabel.text = message
        
        if isHome {
            startAnimation()
        }
    }
    
    
    func loginBtnClick() {
        delegate?.loginBtnClick()
    }
    
    func registBtnClick() {
        delegate?.registBtnClick()
    }
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        //1.添加子控件
        addSubview(iconView)
        addSubview(maskBGView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        //2.布局子控件
        //2.1设置背景
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        //2.2设置小房子
        homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        //2.3设置文本
        messageLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        
        //“哪个控件” 的 “什么属性” “等于” “另一个控件” 的 “什么属性” 乘以 “多少” 加上 “多少”
        let widthCons = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 224)
        addConstraint(widthCons)

        //2.4设置按钮
        registerButton.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width:100,height:30), offset: CGPoint(x: 0, y: 20))
        loginButton.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLabel, size: CGSize(width:100,height:30), offset: CGPoint(x: 0, y: 20))
        
        //2.5设置蒙版
        maskBGView.xmg_Fill(referView: self)
    }
    
    //swift 推荐我们自定义一个控件，要么用纯代码，要么就用xib/storyboard
    required init?(coder aDecoder: NSCoder) {
        
        //如果通过xib/storyboard创建该类，那么会崩溃
        fatalError("init(code:) has not been implemented")
    }
    
    //内部控制方法,转盘动画
    private func startAnimation (){
        //1。创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //2.设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        //该属性默认为yes，代表动画只要执行完毕就移除
        anim.isRemovedOnCompletion = false
        //3.将动画加到图层上
        iconView.layer.add(anim, forKey: nil)
    }
    
    
    //懒加载控件
    //转盘
    private lazy var iconView: UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
        
    }()
    //图标
    private lazy var homeIcon: UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
        
    }()
    //文本
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.textAlignment = NSTextAlignment.center
        label.numberOfLines = 0
        label.textColor = UIColor.lightGray
        label.font = UIFont.systemFont(ofSize: 14)
        return label
        
    }()
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("登录", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        
        btn.addTarget(self, action: #selector(VisitorView.loginBtnClick), for: .touchUpInside)
        
        return btn
        
    }()
    ///注册按钮
    private lazy var registerButton: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("注册", for: .normal)
        btn.setTitleColor(UIColor.orange, for: .normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        btn.addTarget(self, action: #selector(VisitorView.registBtnClick), for: .touchUpInside)
        return btn
        
    }()
    
    private lazy var maskBGView: UIImageView = {
       
        let bg = UIImageView()
        bg.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return bg
    }()
    

}
